import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const TableComponent 		= $Lazy.state(()=>import('./cpt/table-component'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
const ControlModalC 		= $Lazy.state(()=>import('./cpt/control-modal-c'))
const ControlModalReview	= $Lazy.state(()=>import('./cpt/control-modal-review'))

// 账单状态
const BillStatus = [{name:'待审核',value:'45301'},{name:'业务员待确认',value:'45305'}]
// 账单类型
const PayType = [{name:'正常',value:'1'},{name:'冲抵',value:'2'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',				name: 'bill_phase_id', 				type:'select', 		loading:true },
			{ label: '条码号',			    name: 'spec_code' },
			{ label: '医院名称',			name: 'hosp_id', 					type: 'select', 	loading:true },
			{ label: '账单状态',			name: 'status', 					type: 'select', 	data:BillStatus },
			{ label: '标本接收时间',		names: ['start_at','end_at'], 		name:'jz-time',		type: 'date-range', format:1 },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
			{ label:' ', tag: '显示异常账单',	name: 'status_salesman', 	type:'checkbox', colon:false, onValue:({ value })=>{
				this.model.status_salesman = value ? 45306 : ''
			} },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'bill_phase_id', await f.getCache({ api:'billPhaseSelect' } ) ) 
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) ) 
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill/auditlists', {isIndex:true})
	
	// 表单
	cols = [
		{ title: '序号',			dataIndex: 'id', 				...$fn.table.num },
		{ title: '条码号', 			dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '账期', 			dataIndex: 'phase_str', 		width:100 },
		{ title: '标本接收时间', 	dataIndex: 'receive_at', 		...$fn.table.time },
		{ title: '账单类型', 		dataIndex: 'pay_type', 			width:100, render: text => $fn.getState(PayType, text)  },
		{ title: '医院名称', 		dataIndex: 'hosp_name', 		width:200 },
		{ title: '业务员', 			dataIndex: 'salesman_user', 	...$fn.table.name },
		{ title: '标准价格', 		dataIndex: 'sprice', 			...$fn.table.num },
		{ title: '折扣率', 			dataIndex: 'percent', 			...$fn.table.num },
		{ title: '实际价格', 		dataIndex: 'price', 			...$fn.table.num },
		// 新增初始结算价字段 20211018 耿琰
		{ title: '初始结算价',			dataIndex: 'initial_price', 			...$fn.table.num },
		{ title: '创建时间', 		dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '账单状态', 		dataIndex: 'status', 			width:100, render: text => $fn.getState(BillStatus, text) },
		{ title: '备注', 			dataIndex: 'remark', 			width:100 },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text, rows) => {
		return [
			TableControls.click.call(this, '账单信息', ()=> this.modalc.open(rows) )
		]
	}
	
	// 操作按钮
	controls = () => {
		const { forms } = this.state
		return [
			// 确认
			Confirm.call(this, '确认', 'bill/audit',{ idStr:'ids',isKeys:true } ),
			// 一键审核
			Controls.click.call(this, '一键审核', ()=>this.modalreview.open( forms[0].data) ),
			Controls.exports.call(this, 'bill/exportslise', {query:{ area:'1',...this.model}, type: 'post', label:'按业务区域导出', code:'F1', isSock:true} ),	 /* 导出加搜索条件 2021-8-27 --马婷 */
			Controls.exports.call(this, 'bill/exportslise', {query:{ area:'0',...this.model}, type: 'post', label:'按业务员导出', code:'F2', isSock:true} ),
		]
	}
	render(){
		return (
			<>
				<Page title='财务待确认账单' nobc controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'1-today' }) }
					{/* 表格 */}
					<div className='bcf fv ex'>
						{ Table.call(this,{ split:true, isInit: window.isInit,
							onRow:( rows ) => {
								const jzTime = this.searchFormRef.getName('jz-time')
								const timeNull = this.searchFormRef.getName('time-null')
								const times = {
									start_at: jzTime[0],
									end_at: jzTime[1],
									bill_start_at: timeNull ? timeNull[0] : '',
									bill_end_at: timeNull ? timeNull[1] : ''
								}
								this.tableRef.load({...rows, ...times })
							}
						 }) }
					</div>
					{/* 表格 */}
					<div className='bcf fv mt5 ex'>
						<TableComponent onRef={ ref => this.tableRef = ref }/>
					</div>
				</Page>
				{/* 账单信息 */}
				<ControlModalC onRef={ref=>this.modalc=ref} />
				{/* 一键审核 */}
				<ControlModalReview onRef={ref=>this.modalreview=ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index